<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use think\Validate;

class Zarea extends Common
{

	//列表
    public function index()
    {
        $zoning = $this->request->get('zoning');
        $provice = $this->request->get('provice');
        $city = $this->request->get('city');
        $this->assign('zoning',$zoning);
        $this->assign('provice',$provice);
        $this->assign('city',$city);
        //查询分区
        $where = [];
        if($zoning)
            $where['title'] = array('like','%'.$zoning.'%');

        //查询省份
        if($provice){
            $ids = Db::table('cg_market_provice')->where(" title like '%".$provice."%'")->distinct(true)->field('zoning')->select();
            $temp = [];
            foreach($ids as $k=>$v){
                $temp[] = $v['zoning'];
            }
            $temp = implode(',',$temp);
            $where['id'] = array('in',$temp);
        }

        if($city){
            $ids = Db::query("select DISTINCT t2.zoning from cg_market_city t join cg_market_provice t2 on t2.id=t.provice where t.title like '%{$city}%'");
            $ids2 = Db::query("select DISTINCT provice from cg_market_city where title like '%{$city}%'");
            $temp = [];
            $temp2 = [];
            foreach($ids as $k=>$v){
                $temp[] = $v['zoning'];
            }
            foreach($ids2 as $k=>$v){
                $temp2[] = $v['provice'];
            }
            $temp = implode(',',$temp);
            $temp2 = implode(',',$temp2);
            $where['id'] = array('in',$temp);
        }

        $data = Db::table('cg_market_zoning')->where($where)->select();
//        echo Db::table('cg_market_zoning')->getLastSql();
        foreach($data as $k=>$v){
            $where2 = [];
            $where2['zoning'] = $v['id'];
            if($provice){
                $where2['title'] = array('like','%'.$provice.'%');
            }
            if(isset($temp2)){
                $where2['id'] = array('in',$temp2);
            }
            //查询省份及负责人
            $child = Db::table('cg_market_provice')->where($where2)->select();
            $cnt = 0;
            foreach($child as $k1=>$v1){
                $where3 = [];
                $where3['provice'] = $v1['id'];
                if($city){
                    $where3['title'] = array('like','%'.$city.'%');
                }
                $child2 = Db::table('cg_market_city')->where($where3)->select();
                foreach($child2 as $k2=>$v2){

                    //查询该城市下的所有经销商
                    $child2[$k2]['jxss'] = Db::table('cg_jxs')->where('city',$v2['id'])->field('id,title')->select();

                    $child2[$k2]['head']  = Db::table('employee')->where(array('id'=>$v2['head']))->value('em_name');
                    //查询管理人员
                    $v2['admin'] = unserialize($v2['admin']);
                    $temp = implode(',',$v2['admin']);
                    $admins = Db::query("select id, em_name from employee where id in(".$temp.")");
                    $child2[$k2]['admin'] = $admins;
                }
                $child[$k1]['cnt'] = Db::table('cg_market_city')->where(array('provice'=>$v1['id']))->count();
                $child[$k1]['_child'] = $child2;
                $cnt += $child[$k1]['cnt'];
                $child[$k1]['head']  = Db::table('employee')->where(array('id'=>$v1['head']))->value('em_name');
                //查询管理人员
                $v1['admin'] = unserialize($v1['admin']);
                $temp = implode(',',$v1['admin']);
                $admins = Db::query("select id, em_name from employee where id in(".$temp.")");
                $child[$k1]['admin'] = $admins;

            }
            $data[$k]['cnt'] = $cnt;
            $data[$k]['_child'] = $child;
            $data[$k]['head']  = Db::table('employee')->where(array('id'=>$v['head']))->value('em_name');
            //查询管理人员
            $v['admin'] = unserialize($v['admin']);
            $temp = implode(',',$v['admin']);
            $admins = Db::query("select id, em_name from employee where id in(".$temp.")");
            $data[$k]['admin'] = $admins;
        }
        $this->assign('data',$data);

        return $this->fetch('index');
    }

}
